Passed
Push — master ( c61efe...c9bfc4 )
by Roy
01:46
created

show-paths.local.deno.ts ➔ stringifyConverter   A

Complexity

Conditions 3

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 18
rs 9.65
c 0
b 0
f 0
cc 3
1
/* eslint-env es6, deno */
2
// # spell-checker:ignore Deno
3
4
/* eslint-disable no-console , functional/immutable-data , security/detect-object-injection, security-node/detect-crlf , @typescript-eslint/ban-ts-comment , @typescript-eslint/no-explicit-any */
5
6
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
7
/// <reference path='../src/types/deno.d.ts'/>
8
9
// @ts-ignore
10
import osPaths from '../src/mod.deno.ts';
11
12
// create a local reference to refer to `Deno` (for better linting without need for multiple `// @ts-ignore` directives)
13
// @ts-ignore
14
const deno = Deno;
15
16
function objectEntries(obj: any) {
17
	const map: any = {};
18
	Object.keys(obj).forEach((key) => {
19
		const value = obj[key];
20
		const val = typeof value === 'function' ? value() : value;
21
		map[key] = val;
22
	});
23
	return map;
24
}
25
26
function stringifyConverter(_key: any, val: any) {
27
	if (typeof val === 'function') {
28
		/* eslint-disable functional/no-let */
29
		let p = '[Function: ' + val.name + ']';
30
		let s = '';
31
		let m = '';
32
		Object.keys(val).forEach((k) => {
33
			if (m === '') {
34
				p += ' { ';
35
				s += ' }';
36
			} else m += ', ';
37
			m += k + ': ' + JSON.stringify(val[k], stringifyConverter);
38
		});
39
		return p + m + s;
40
		/* eslint-enable functional/no-let */
41
	}
42
	return val;
43
}
44
45
console.log({ osPaths });
46
console.log({ osPaths: JSON.parse(JSON.stringify(osPaths, stringifyConverter)) }); // custom output; // maint:[2020-01-30; rivy] await resolution of <https://github.com/denoland/deno/issues/9333> by <https://github.com/denoland/deno/pull/9363>
47
console.log(objectEntries(osPaths));
48
console.log('home() =', osPaths.home());
49
console.log('temp() =', osPaths.temp());
50
51
deno.env.set('TEMP', 'temp');
52
deno.env.set('TMPDIR', 'tmpdir');
53
deno.env.set('TMP', 'tmp');
54
console.log(objectEntries(osPaths));
55
56
deno.env.set('TEMP', 'NEW');
57
console.log(objectEntries(osPaths));
58
59
/* eslint-enable no-console , functional/immutable-data , security/detect-object-injection, security-node/detect-crlf , @typescript-eslint/ban-ts-comment , @typescript-eslint/no-explicit-any */
60